package cn.hs.ph.servlet;

import cn.hs.ph.dao.impl.CourierStationDaoImpl;
import cn.hs.ph.entity.CourierStation;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/courierstation")
public class CourierStationServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        CourierStationDaoImpl courierStationDao = new CourierStationDaoImpl();
        //        分类处理请求
        //查询
        if ("selectAll".equals(action)) {
//        请求参数
            String courierStationName = req.getParameter("courierStationName");
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
//        参数封装
            Map<String, String> params = new HashMap<>();
            params.put("courierStationName", courierStationName);
            params.put("courierStationCommunityName", courierStationCommunityName);
//        分页 / 时间
            params.put("pageNum", pageNum);
            params.put("pageSize", pageSize);
            params.put("startTime", startTime);
            params.put("endTime", endTime);
//        总条数
            int total = courierStationDao.selectCount(params);
            List<CourierStation> list = courierStationDao.selectAll(params);
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("list", list);
            resMap.put("total", total);
            Result data = Result.success(resMap);
            resp.getWriter().write(JSON.toJSONString(data));
        }
        //添加
        else if ("add".equals(action)) {
            //获取参数
            String courierStationName = req.getParameter("courierStationName");
            String courierStationProvince = req.getParameter("courierStationProvince");
            String courierStationCity = req.getParameter("courierStationCity");
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String CommunityId = req.getParameter("courierStationCommunityId");

            int courierStationCommunityId = 0;
            if (CommunityId!=null && !"".equals(CommunityId))
                courierStationCommunityId=Integer.parseInt(CommunityId);
            //封装参数
            CourierStation courierStation = new CourierStation();
            courierStation.setCourierStationName(courierStationName);
            courierStation.setCourierStationProvince(courierStationProvince);
            courierStation.setCourierStationCity(courierStationCity);
            courierStation.setCourierStationDistrict(courierStationDistrict);
            courierStation.setCourierStationAddress(courierStationAddress);
            courierStation.setCourierStationCommunityName(courierStationCommunityName);
            courierStation.setCourierStationCommunityId(courierStationCommunityId);
            //调用方法
            int num = courierStationDao.insert(courierStation);
            Result res=null;
            if(num!=0){
                res=Result.success();
            }else {
                res=Result.error();
            }
            resp.getWriter().write(JSON.toJSONString(res));
        }
        //修改
        else if("modify".equals(action)){
            //获取参数
            String courierStationName = req.getParameter("courierStationName");
            String courierStationProvince = req.getParameter("courierStationProvince");
            String courierStationCity = req.getParameter("courierStationCity");
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String CommunityId = req.getParameter("courierStationCommunityId");
            String id = req.getParameter("courierStationId");
            int courierStationCommunityId = 0;
            if (CommunityId!=null && !"".equals(CommunityId))
                courierStationCommunityId=Integer.parseInt(CommunityId);
            int courierStationId=Integer.parseInt(id);
            //封装参数
            CourierStation courierStation = new CourierStation();
            courierStation.setCourierStationName(courierStationName);
            courierStation.setCourierStationProvince(courierStationProvince);
            courierStation.setCourierStationCity(courierStationCity);
            courierStation.setCourierStationDistrict(courierStationDistrict);
            courierStation.setCourierStationAddress(courierStationAddress);
            courierStation.setCourierStationCommunityName(courierStationCommunityName);
            courierStation.setCourierStationCommunityId(courierStationCommunityId);
            courierStation.setCourierStationId(courierStationId);
            //调用方法
            int num = courierStationDao.update(courierStation);
            Result res=null;
            if(num!=0){
                res=Result.success();
            }else {
                res=Result.error();
            }
            //向前端传数据
            resp.getWriter().write(JSON.toJSONString(res));
        }
        //修改上下架状态
        else if("modifyStatus".equals(action)){
            //获取参数
            String id = req.getParameter("courierStationId");
            int courierStationId = 0;
            if(id!=null && !"".equals(id)){
                 courierStationId=Integer.parseInt(id);
            }
            String status = req.getParameter("courierStationStatus");
            int courierStationStatus = 0;
            if(status!=null && !"".equals(status)){
                 courierStationStatus=Integer.parseInt(status);
            }
            CourierStation courierStation=new CourierStation();
            courierStation.setCourierStationId(courierStationId);
            courierStation.setCourierStationStatus(courierStationStatus);
            int num = courierStationDao.updateStatus(courierStation);
            Result res;
            if(num!=0){
                res=Result.success();
            }else {
                res=Result.error();
            }
            resp.getWriter().write(JSON.toJSONString(res));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
